//
//  StatusCell.swift
//  WeiBo_NB
//
//  Created by apple on 15/3/10.
//  Copyright (c) 2015年 Ai. All rights reserved.
//

import UIKit

class StatusCell: UITableViewCell {

  
    
    // 头像
    @IBOutlet weak var iconImage: UIImageView!
    // 身份认证（徽章）
    @IBOutlet weak var verfiedImag:UIImageView!
    // VIP级别
    @IBOutlet weak var vipIcon: UIImageView!
    // 姓名
    @IBOutlet weak var nameLabel: UILabel!
    // 正文
    @IBOutlet weak var contentLabel: UILabel!
    //发布时间
    @IBOutlet weak var postTime: UILabel!
    // 发布设备
    @IBOutlet weak var deviceOrigin: UILabel!
    
    // View布局高度
    @IBOutlet weak var viewLayoutHeight: NSLayoutConstraint!
    // View布局宽度
    @IBOutlet weak var viewLayoutWidth: NSLayoutConstraint!
    // cell布局设置
    @IBOutlet weak var ConnectionViewLayout: UICollectionViewFlowLayout!
    
    // 图片视图
    @IBOutlet weak var picView: UICollectionView!
    // 底部视图
    @IBOutlet weak var footView: UIView!
    // 转发微博正文
    @IBOutlet weak var retweetText: UILabel!
    
    var status : Status?{
        didSet{
            // 属性分发
            nameLabel.text = status!.user!.name
            contentLabel.text = status!.text
            postTime.text = status!.created_at
            deviceOrigin.text = status!.source
            // 如果有转发微博，设置转发微博文字
            if status?.retweeted_status?.text != nil{
                retweetText.text = status!.retweeted_status!.user!.name! + " : " + status!.retweeted_status!.text!

            }
            
            // 头像设置
            if let iconUrl = status?.user?.profile_image_url{
              netWorkManager.sharedManager.requestImage(iconUrl, completion: { (result, error) -> () in
                if let image = result as? UIImage{
                    self.iconImage.image = image
                }
              })
            }
            
            // 会员和徽章设置
            verfiedImag.image = status?.user?.verifiedImage
            vipIcon.image = status?.user?.mbImage
            
            // 图片大小设置
            let Psize = picSize()
            viewLayoutHeight.constant = Psize.viewSize.height
            viewLayoutWidth.constant = Psize.viewSize.width
            ConnectionViewLayout.itemSize = Psize.itemSize
            
            // 刷新视图，重新布局
            picView.reloadData()
         
        }
    }
    
    ///  根据底部视图计算行高
  
    func cellHeight(status:Status)->CGFloat{
        self.status = status
        layoutIfNeeded()
        return CGRectGetMaxY(footView.frame)
    }
    
    
    
    ///  计算微博图片的大小
    ///
    ///  :returns: viewSize是视图大小；itemSize是cell大小
    func picSize()->(viewSize:CGSize,itemSize:CGSize){
        let temp: CGFloat = 90
        let margin:CGFloat = 10
        var itemSize = CGSizeMake(temp, temp)
        // 配图视图大小
        var viewSize = CGSizeZero

        var count = status?.picURLS?.count ?? 0
        
        if count == 0{
            return (viewSize,itemSize)
        }
        if count == 1{
            var path = netWorkManager.sharedManager.fullPath(status!.picURLS![0].thumbnail_pic!)
            if let image = UIImage(contentsOfFile: path){
                return (image.size,image.size)
            }else{
                return (viewSize,itemSize)
            }
        }
        if count == 4{
            viewSize = CGSizeMake(temp * 2 + margin, temp * 2 + margin)
        }else{
            /** 当count==2.3.   row 1
                        5.6        2
                        .7.8.9..    3
            */
            var row = (count - 1)/3
            viewSize = CGSizeMake(3 * temp + 2 * margin, CGFloat((row + 1)) * temp + CGFloat(row) * margin)
        }
        if count == 2{
            viewSize = CGSizeMake(2 * temp + margin , temp )
        }
        return (viewSize,itemSize)
        
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 最大行宽度
        contentLabel.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 30
        retweetText?.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.size.width - 30
    }
}

// UIcollectionView的数据源方法
extension StatusCell:UICollectionViewDataSource{
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return status?.picURLS?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PICCELL", forIndexPath: indexPath) as! PicCell
        cell.urlString = status!.picURLS![indexPath.item].thumbnail_pic
        
        return cell
    }
}

// 自定义CollectionViewcell
class PicCell : UICollectionViewCell {
    
    @IBOutlet weak var picImage: UIImageView!
    
    var urlString:String? {
        didSet{
            // 直接从缓存里取出图片赋值给PicImage
            let path = netWorkManager.sharedManager.fullPath(urlString!)
            picImage.image = UIImage(contentsOfFile: path)
            
        }
    }
    
}











